package top.zy.service.zymallgoodsservice.spec.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import top.zy.common.zymallcommon.utils.constants.Constants;
import top.zy.common.zymallcommon.utils.exception.ZymalException;
import top.zy.service.zymallgoodsinterface.entity.SpecGroup;
import top.zy.service.zymallgoodsinterface.entity.SpecParam;
import top.zy.service.zymallgoodsinterface.entity.Specification;
import top.zy.service.zymallgoodsinterface.vo.SpecGroupVo;
import top.zy.service.zymallgoodsservice.spec.service.SpecService;

import java.util.List;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 10:42 2019/6/24
 */
@RestController
@RequestMapping("spec")
public class SpecController {

    @Autowired
    @Qualifier("specServiceImpl")
    private SpecService specService;

    /***
     * 根据分类id查询规格组信息
     * @param id
     * @return
     */
    @GetMapping("groups/{id}")
    public List<SpecGroup> querySpecGroupByCategoryId(@PathVariable("id") Long id){
        List<SpecGroup> specGroupList= specService.querySpecGroupByCategoryId(id);
       if (CollectionUtils.isEmpty(specGroupList)){
           throw new ZymalException("该分类没有规格组",Constants.RESP_STATUS_INTERNAL_ERROR);
       }
       return specGroupList;
    }

    /**
     * 根据规格组信息查询 参数
     * @param gid
     * @param cid
     * @param searching
     * @return
     */
  @GetMapping("params")
  public List<SpecParam> queryParams(@RequestParam(value = "gid", required = false) Long gid,
                                     @RequestParam(value = "cid", required = false) Long cid,
                                     @RequestParam(value = "searching", required = false) Boolean searching){
     List<SpecParam> specParamList= specService.queryParams(gid,cid,searching);
      if (CollectionUtils.isEmpty(specParamList)){
          throw new ZymalException("该规格组下没有参数",Constants.RESP_STATUS_INTERNAL_ERROR);
      }
      return specParamList;
  }
    /***
     * 根据分类查询规格组信息和其包含的参数信息
     * @param cid
     * @return
     */
    @GetMapping("group")
    List<SpecGroupVo> querySpecGroupAndParamByCid(@RequestParam("cid")Long cid){
        return specService.querySpecGroupAndParamByCid(cid);
    }
}
